#数据库监控工具2.0
import tkinter as tk
from tkinter import ttk, messagebox, simpledialog
import os
import cx_Oracle
import time
import threading
from tkinter.ttk import Treeview

class demo(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("数据库监控工具")
        self.geometry("1200x400")
        self.resizable(True, True)
        #self.attributes("-alpha", 0.7)

        # 创建笔记本卡片
        self.notebook = ttk.Notebook(self)
        self.notebook.pack(expand=True, fill="both")

        # 创建查询卡片
        self.sttscx = tk.Frame(self.notebook)
        self.notebook.add(self.sttscx, text="监控事项")
        self.sttscx_list = Treeview(self.sttscx,columns=('c1', 'c2', 'c3',
                         'c4', 'c5', 'c6','c7'),
                show="headings")
        self.sttscx_list.pack(expand=True, fill="both")
        # 设置每列宽度和对齐方式
        self.sttscx_list.column('c1', width=100, anchor='center')
        self.sttscx_list.column('c2', width=300, anchor='center')
        self.sttscx_list.column('c3', width=200, anchor='center')
        self.sttscx_list.column('c4', width=300, anchor='center')
        self.sttscx_list.column('c5', width=100, anchor='center')
        self.sttscx_list.column('c6', width=100, anchor='center')
        self.sttscx_list.column('c7', width=100, anchor='center')
        # 设置每列表头标题文本
        self.sttscx_list.heading('c1', text='事项')
        self.sttscx_list.heading('c2', text='事项')
        self.sttscx_list.heading('c3', text='事项')
        self.sttscx_list.heading('c4', text='事项')
        self.sttscx_list.heading('c5', text='事项')
        self.sttscx_list.heading('c6', text='事项')
        self.sttscx_list.heading('c7', text='事项')
        self.sttscx_list.pack(side=tk.LEFT, fill=tk.Y)

        # 创建事项查询卡片
        self.qrnrksxcx = tk.Frame(self.notebook)
        self.notebook.add(self.qrnrksxcx, text="监控事项")
        self.qrnrksxcx_list = Treeview(self.qrnrksxcx, columns=('c1', 'c2', 'c3',
                                                          'c4', 'c5', 'c6', 'c7'),
                                    show="headings")
        self.qrnrksxcx_list.pack(expand=True, fill="both")
        # 设置每列宽度和对齐方式
        self.qrnrksxcx_list.column('c1', width=100, anchor='center')
        self.qrnrksxcx_list.column('c2', width=300, anchor='center')
        self.qrnrksxcx_list.column('c3', width=150, anchor='center')
        self.qrnrksxcx_list.column('c4', width=300, anchor='center')
        self.qrnrksxcx_list.column('c5', width=100, anchor='center')
        self.qrnrksxcx_list.column('c6', width=100, anchor='center')
        self.qrnrksxcx_list.column('c7', width=150, anchor='center')
        # 设置每列表头标题文本
        self.qrnrksxcx_list.heading('c1', text='事项')
        self.qrnrksxcx_list.heading('c2', text='事项')
        self.qrnrksxcx_list.heading('c3', text='事项')
        self.qrnrksxcx_list.heading('c4', text='事项')
        self.qrnrksxcx_list.heading('c5', text='事项')
        self.qrnrksxcx_list.heading('c6', text='事项')
        self.qrnrksxcx_list.heading('c7', text='事项')
        self.qrnrksxcx_list.pack(side=tk.LEFT, fill=tk.Y)

        # 创建查询卡片
        self.jsydtj = tk.Frame(self.notebook)
        self.notebook.add(self.jsydtj, text="事项")
        self.jsydtj_list = Treeview(self.jsydtj, columns=('c1', 'c2', 'c3',
                                                                'c4', 'c5'),
                                       show="headings")
        self.jsydtj_list.pack(expand=True, fill="both")
        # 设置每列宽度和对齐方式
        self.jsydtj_list.column('c1', width=200, anchor='center')
        self.jsydtj_list.column('c2', width=400, anchor='center')
        self.jsydtj_list.column('c3', width=200, anchor='center')
        self.jsydtj_list.column('c4', width=200, anchor='center')
        self.jsydtj_list.column('c5', width=200, anchor='center')

        # 设置每列表头标题文本
        self.jsydtj_list.heading('c1', text='事项')
        self.jsydtj_list.heading('c2', text='事项')
        self.jsydtj_list.heading('c3', text='事项')
        self.jsydtj_list.heading('c4', text='事项')
        self.jsydtj_list.heading('c5', text='事项')
        self.jsydtj_list.pack(side=tk.LEFT, fill=tk.Y)
        #事项
        self.wrksx = tk.Frame(self.notebook)
        self.notebook.add(self.wrksx, text="监控事项")
        self.wrksx_list = Treeview(self.wrksx, columns=('c1', 'c2', 'c3',
                                                          'c4', 'c5', 'c6', 'c7'),
                                    show="headings")
        self.wrksx_list.pack(expand=True, fill="both")
        # 设置每列宽度和对齐方式
        self.wrksx_list.column('c1', width=100, anchor='center')
        self.wrksx_list.column('c2', width=300, anchor='center')
        self.wrksx_list.column('c3', width=200, anchor='center')
        self.wrksx_list.column('c4', width=300, anchor='center')
        self.wrksx_list.column('c5', width=100, anchor='center')
        self.wrksx_list.column('c6', width=100, anchor='center')
        self.wrksx_list.column('c7', width=100, anchor='center')
        # 设置每列表头标题文本
        self.wrksx_list.heading('c1', text='事项')
        self.wrksx_list.heading('c2', text='事项')
        self.wrksx_list.heading('c3', text='事项')
        self.wrksx_list.heading('c4', text='事项')
        self.wrksx_list.heading('c5', text='事项')
        self.wrksx_list.heading('c6', text='事项')
        self.wrksx_list.heading('c7', text='事项')
        self.wrksx_list.pack(side=tk.LEFT, fill=tk.Y)

        self.fetch_data_and_update_tree()
        self.refresh_tree()

    def connect_to_oracle(self):
            # 这里填写你的数据库连接信息
            conn = cx_Oracle.connect()
            return conn

            # 执行查询并更新Treeview的函数
    def fetch_data_and_update_tree(self):
            conn = self.connect_to_oracle()
            cursor = conn.cursor()
            sql1 = ""
            sql2 = ""
            sql3 = ""
            sql4 = ""
            cursor.execute(sql1)  # 替换为你的表名和列名
            rows = cursor.fetchall()
            cursor.execute(sql2)
            rows2 = cursor.fetchall()
            cursor.execute(sql3)
            rows3 = cursor.fetchall()
            cursor.execute(sql4)
            rows4 = cursor.fetchall()

            # 清空Treeview中的现有项
            for item in self.sttscx_list.get_children():
                self.sttscx_list.delete(item)

                # 插入新数据到Treeview
            for row in rows:
                self.sttscx_list.insert("", "end", values=(row[0], row[1], row[2],row[3],row[4], row[5], row[6]))

            for item in self.qrnrksxcx_list.get_children():
                self.qrnrksxcx_list.delete(item)

                # 插入新数据到Treeview
            for row in rows2:
                self.qrnrksxcx_list.insert("", "end", values=(row[0], row[1], row[2],row[3],row[4], row[5], row[6]))

            for item in self.jsydtj_list.get_children():
                self.jsydtj_list.delete(item)

                # 插入新数据到Treeview
            for row in rows3:
                self.jsydtj_list.insert("", "end", values=(row[0], row[1], row[2],row[3],row[4]))

            for item in self.wrksx_list.get_children():
                self.wrksx_list.delete(item)

                # 插入新数据到Treeview
            for row in rows4:
                self.wrksx_list.insert("", "end", values=(row[0], row[1], row[2],row[3],row[4], row[5], row[6]))

            cursor.close()
            conn.close()

            # 刷新Treeview的函数（递归调用以定期更新）

    def refresh_tree(self):
            self.fetch_data_and_update_tree()
            # 5000毫秒后再次调用refresh_tree
            self.after(5000, lambda: self.refresh_tree())

if __name__ == "__main__":
    app = demo()
    app.mainloop()
